//
//  BCNPrefs.m
//  BCN Emergencies
//
//  Created by Ana Martínez on 12/15/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "BCNPrefs.h"

#define FIRSTTIMEKEY @"FirstTimeUsageApp"
#define NAMEKEY @"Name"
#define LASTNAME @"LastName"
#define TELEPHONESKEY @"TelephonesArray"
#define LANGUAGEKEY @"Language"
#define IDENTIFIERTYPEKEY @"IdentifierType"
#define IDENTIFIERNUMBERKEY @"IdentifierNumber"
#define NUMBERKEY @"PhoneNumber"

#define PATH @"/Documents/prefs.plist"

@implementation BCNPrefs 

@synthesize name;
@synthesize language;
@synthesize lastname;
@synthesize telephones;
@synthesize isFirstLaunch;
@synthesize identifierType;
@synthesize identifierNumber;
@synthesize number;

static BCNPrefs *sharedSingleton = nil;

- (id)init {
	if ((self = [super init])) {
		
        [self loadPrefs];
		
	}
	return self;
}

+ (BCNPrefs *) getInstance {
	@synchronized(self) 	{
		if (sharedSingleton == nil) {
			sharedSingleton = [[BCNPrefs alloc] init];
		}
	}
	return sharedSingleton;
}

- (void) loadPrefs {
	NSLog(@"Loading prefs");
	NSString *path = [NSHomeDirectory() stringByAppendingString: PATH];
	NSLog(@"Reading prefs in %@", path);
	
	NSDictionary *dictionary = nil;
	if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
		dictionary = [NSDictionary dictionaryWithContentsOfFile: path];
	}
	if (dictionary == nil) {
		isFirstLaunch = YES;
	} else {
		// First time usage Application
		NSString *firstTime = [dictionary objectForKey:FIRSTTIMEKEY];
		
		if (firstTime != nil){ 
			isFirstLaunch = [firstTime boolValue];	
			if(isFirstLaunch){
				NSLog(@"firstTimeUsageApp!");
			}else{
				NSLog(@"NO firstTimeUsageApp!");
                self.name = [dictionary objectForKey:NAMEKEY];
                self.lastname = [dictionary objectForKey:LASTNAME];
                self.identifierType = [dictionary objectForKey:IDENTIFIERTYPEKEY];
                self.identifierNumber = [dictionary objectForKey:IDENTIFIERNUMBERKEY];
                self.telephones = [dictionary objectForKey:TELEPHONESKEY];
                self.number = [dictionary objectForKey:NUMBERKEY];

			}
			
		}else{ 
			isFirstLaunch = YES;
			NSLog(@"firstTimeUsageApp 2!");
		}
        
        	}
}

- (void) savePrefs {
	NSLog(@"Saving prefs");
	NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
	
	NSString *firstTime = [NSString stringWithFormat:@"%d", self.isFirstLaunch ];
	NSString *lang = [NSString stringWithFormat:@"%d", self.language];
    
	
	[dictionary setObject: firstTime forKey: FIRSTTIMEKEY];
    [dictionary setObject: lang forKey:LANGUAGEKEY];
    if (self.name != nil) 
        [dictionary setObject: self.name forKey:NAMEKEY];
    if (self.lastname != nil) 
        [dictionary setObject: self.lastname forKey:LASTNAME];
    if (self.identifierType != nil) 
        [dictionary setObject: self.identifierType forKey:IDENTIFIERTYPEKEY];
    if (self.identifierNumber != nil) 
        [dictionary setObject: self.identifierNumber forKey:IDENTIFIERNUMBERKEY];
    if (self.telephones != nil) 
        [dictionary setObject: self.telephones forKey:TELEPHONESKEY];
    if (self.number != nil) 
        [dictionary setObject: self.number forKey:NUMBERKEY];
    
	NSString *path = [NSHomeDirectory() stringByAppendingString: PATH];
	[dictionary writeToFile:path atomically:YES];
    
}


@end
